package cn.com.cpic.insurance.rulecheck.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.cpic.insurance.rulecheck.entity.RuleItem;
import cn.com.cpic.insurance.rulecheck.service.RuleItemService;

@Results({ @Result(name = CrudActionSupport.RELOAD, location = "rule-item.action", type = "redirect", params = {
		"ruleId", "${ruleId}", "conditionId", "${conditionId}" }) })
public class RuleItemAction extends CrudActionSupport<RuleItem> {

	private static final long serialVersionUID = -5086505209066238266L;

	private RuleItemService service;

	private Long id;

	private Long conditionId;

	private Long ruleId;

	private RuleItem entity;

	private List<RuleItem> items;

	public String execute() {
		return list();
	}

	public String list() {
		items = service.getRuleItemList(ruleId);
		return SUCCESS;
	}

	public String input() {
		prepareModel();
		return INPUT;
	}

	public String save() {
		service.saveRuleItem(this.entity);
		return RELOAD;
	}

	public String delete() {
		service.deleteRuleItem(this.id);
		return RELOAD;
	}

	protected void prepareModel() {
		if (id != null) {
			this.entity = service.getRuleItem(id);
		} else {
			this.entity = new RuleItem();
			this.entity.setRuleId(this.ruleId);
		}
	}

	@Autowired
	public void setService(RuleItemService service) {
		this.service = service;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getConditionId() {
		return conditionId;
	}

	public void setConditionId(Long conditionId) {
		this.conditionId = conditionId;
	}

	public void setRuleId(Long ruleId) {
		this.ruleId = ruleId;
	}

	public Long getRuleId() {
		return ruleId;
	}

	public void setRuleId(String ruleDefId) {
		if (StringUtils.isNotBlank(ruleDefId)) {
			this.ruleId = Long.parseLong(ruleDefId);
		}
	}

	public List<RuleItem> getItems() {
		return items;
	}

	public RuleItem getModel() {
		return entity;
	}
}
